package com.hevs.samplewebapp.server.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hevs.samplewebapp.server.entities.MedicalRecord;
import com.hevs.samplewebapp.server.entities.User;

/**
 * Mock DAO for the Medical Records
 * 
 * @author Arnaud Durand
 * @date 08.03.12
 */
public class MedicalRecordDAOMock implements MedicalRecordDAO {

        private Map<Key, MedicalRecord> medicalRecords = new HashMap<Key, MedicalRecord>();

        private long seed = 1;
        
        public MedicalRecordDAOMock() {
                // sample datas
                
                populate(new MedicalRecord(4.99, new Date(1325372400000L), 0, 2, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAww")));
                populate(new MedicalRecord(6.03, new Date(1325458800000L), 0, 2, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAww")));
                populate(new MedicalRecord(5.78, new Date(1325545200000L), 0, 2, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYAww")));
                
                populate(new MedicalRecord(25.025, new Date(1325372400000L), 1, 3, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYBAw")));
                populate(new MedicalRecord(13.702, new Date(1325372400000L), 2, 11, KeyFactory.stringToKey("aglub19hcHBfaWRyCgsSBFVzZXIYBQw")));
        }

        
        private void populate(MedicalRecord medicalRecord) {
                populate(medicalRecord, (KeyFactory.createKey(
                                MedicalRecord.class.getSimpleName(), seed)));
        }
        
        private void populate(MedicalRecord medicalRecord, Key key) {
                seed++;
                /*medicalData.setKey(KeyFactory.createKey(
                                Employee.class.getSimpleName(), seed));*/
                
                medicalRecord.setKey(key);
                medicalRecord.setTimestamp(new Date());
                medicalRecords.put(medicalRecord.getKey(), medicalRecord);
        }

        @Override
        public List<MedicalRecord> getAll() {
                return new ArrayList<MedicalRecord>(medicalRecords.values());
        }
        
        @Override
        public MedicalRecord getMedicalRecord(Key key) {
                return medicalRecords.get(key);
        }

        @Override
        public void update(MedicalRecord medicalRecord) {
                if (medicalRecord.getKey() == null ) {
                        populate(medicalRecord);
                } else {
                        medicalRecords.put(medicalRecord.getKey(), medicalRecord);
                }
                
        }

        @Override
        public void remove(MedicalRecord medicalData) {
                medicalRecords.remove(medicalData.getKey());
                
        }

        @Override
        public void add(List<MedicalRecord> medicalDatas) {

                // obtain an Iterator for Collection
                Iterator<MedicalRecord> itr = medicalDatas.iterator();

                MedicalRecord medicalData;
                // iterate through HashMap values iterator
                while (itr.hasNext()) {
                        medicalData = itr.next();
                        populate(medicalData);
                }
        
        }

        @Override
        public List<MedicalRecord> getAll(User user) {
                
                List<MedicalRecord> userRecords = new ArrayList<MedicalRecord>();
                
                for (MedicalRecord medicalRecord : medicalRecords.values()) {
                
                        if(medicalRecord.getUser().equals(user.getKey()))
                                userRecords.add(medicalRecord);
                }
                
                return userRecords;
        }

}
